#include "reg51.h"

#define ADDR0 P1_0
#define ADDR1 P1_1
#define ADDR2 P1_2
#define ADDR3 P1_3
#define ENLED P1_4

// 用数组来存储数码管的真值表
__code unsigned char LedChar[] = {
        0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
        0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};

int main() {
    ENLED = 0;
    ADDR3 = 1;
    ADDR2 = 0;
    ADDR1 = 0;
    ADDR0 = 0;

    TMOD = 0x01;    // 设置定时器 T0 为工作模式 1
    TH0 = 0xB8;     // 定时 20ms
    TL0 = 0x00;
    TR0 = 1;        // 开始计数

    unsigned char cnt = 0;
    unsigned char second = 0;

    while (1) {
        if (TF0 != 1) {
            continue;
        }

        TF0 = 0;
        TH0 = 0xB8;
        TL0 = 0x00;

        if (cnt++ < 50) {
            continue;
        }

        cnt = 0;
        P0 = LedChar[second++];
        if (second >= 16) {
            second = 0;
        }
    }

}
